package org.bedracket.forge;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraftforge.event.entity.living.ZombieEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import org.bedracket.event.ZombieReinforcementEvent;
import org.bukkit.entity.LivingEntity;

@Mod.EventBusSubscriber(value = Side.SERVER, modid = "mohist")
public class BedRacketEventHandler {

    @SubscribeEvent(receiveCanceled = true)
    public static void onZombieSummon(ZombieEvent.SummonAidEvent event) {
        boolean spawnReinforcements = false;
        EntityZombie zombie = event.getCustomSummonedAid();
        EntityLivingBase livingBase = event.getAttacker();
        final double chance = zombie.getEntityAttribute(EntityZombie.getSRChance()).getAttributeValue();
        ZombieReinforcementEvent bukkitEvent = new ZombieReinforcementEvent(zombie.getBukkitEntity(), (LivingEntity) livingBase.getBukkitEntity(), chance);
        if (bukkitEvent.callEvent()) {
            spawnReinforcements = zombie.getEntityRandom().nextFloat() < bukkitEvent.getChance();
        }
        event.setCanceled(!spawnReinforcements);
    }

}
